package com.learn.compare;

import java.util.Arrays;
import java.util.Comparator;

public class Goods2 {
    public static void main(String[] args) {
        Goods[] goods = new Goods[5];
        goods[0] = new Goods("aaa", 33);
        goods[1] = new Goods("bbb", 21);
        goods[2] = new Goods("ccc", 84);
        goods[3] = new Goods("ddd", 35);

        Arrays.sort(goods, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                if(o1 instanceof Goods && o2 instanceof Goods){
                    Goods g1 = (Goods) o1;
                    Goods g2 = (Goods) o2;
                    if(g1.getName().equals(g2.getName())){
                        return -Double.compare(g1.getPrice(), g2.getPrice());

                    }else {
                        return g1.getName().compareTo(g2.getName());
                    }
                }
                throw new RuntimeException("输入类型不匹配");
            }
        });
    }
}
